// 单词翻转 "hello    tencent  " [表情] "tencent hello"
// 1 js原生api
function fn(str) {
    return str.split(' ').filter(item => item !== '').reverse().join(' ')
}

console.log(fn('hello    tencent  '))


// 2 栈 push pop substring
function fun(str) {
    str += ' '
    const stack = [], len = str.length
    for (let i = 0, j = 0; j < len; j++) {
        if (str[j] === ' ' && i !== j) {
            stack.push(str.substring(i, j))
            while (str[++j] === ' ') { }
            i = j
        }
    }
    let result = ''
    while(stack.length !== 0) {
        result += stack.pop() + ' '
    }
    return result.substring(0, result.length - 1)
}

console.log(fun('hello    tencent  '))